/// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Microsoft.VSPowerToys.Common.TaskManager.ContentInstaller
{
    /// <summary>
    /// This dialog is represented to user to resolve copy conflicts.
    /// </summary>
    public partial class CopyConflictDialog : Form
    {

        /// <summary>
        /// Available options for conflict resolution
        /// </summary>
        public enum ConflictResolution
        {
            None = 0, Overwrite, SaveNew, Skip
        }

        /// <summary>
        /// Gets the option selected by the user
        /// </summary>
        public ConflictResolution SelectedOption
        {
            get {
                if (this.uxOverwrite.Checked) return ConflictResolution.Overwrite;
                if (this.uxNew.Checked) return ConflictResolution.SaveNew;
                return ConflictResolution.Skip;
            }
        }

        private static ConflictResolution savedSetting = ConflictResolution.None;

        /// <summary>
        /// Gets or sets the value saved by the user for this session.
        /// </summary>
        public static ConflictResolution SavedSetting
        {
            get { return savedSetting; }
            set { savedSetting = value; }
        }

        public CopyConflictDialog(string fullPath)
        {
            InitializeComponent();
            this.uxErrorText.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture,
                Microsoft.VSPowerToys.Common.TaskManager.Strings.FileAlreadyExistsLabel,
                fullPath);
            this.uxNew.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture,
                Microsoft.VSPowerToys.Common.TaskManager.Strings.CopyAsNewLabel,
                Path.GetFileName(GetNextAvailableFileName(fullPath)));
        }

        private void uxContinueButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            if (this.uxSaveSetting.Checked)
            {
                savedSetting = this.SelectedOption;
            }
            this.Close();
        }

        /// <summary>
        /// Gets the next available file name by incremeting an index
        /// </summary>
        /// <param name="fullpath">Full path to the file</param>
        /// <returns>Full path to the new file</returns>
        public static string GetNextAvailableFileName(string fullPath)
        {
            string fileName = Path.GetFileNameWithoutExtension(fullPath);
            string extension = Path.GetExtension(fullPath);
            string directory = Path.GetDirectoryName(fullPath);
            int index = 1;
            while (File.Exists(fullPath))
            {
                string newFileName = fileName + "(" + index + ")";
                index += 1;
                fullPath = Path.Combine(directory, newFileName + extension);
            }
            return fullPath;
        }
    }
}